import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
  ListToolsRequestSchema,
  CallToolRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";

const server = new Server(
  {
    name: "tools-server",
    version: "1.0.0",
  },
  {
    capabilities: {
      tools: {},
    },
  }
);

server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: "sum",
        description: "计算两个数字的和",
        inputSchema: {
          type: "object",
          properties: {
            a: { type: "number", description: "加数 A" },
            b: { type: "number", description: "加数 B" },
          },
          required: ["a", "b"],
        },
      },
    ],
  };
});

server.setRequestHandler(CallToolRequestSchema, async (requests) => {
  const { name: toolName, arguments: args } = requests.params;

  if (toolName === "sum") {
    const { a, b } = args;
    const result = a + b;

    return {
      content: [
        {
          type: "text",
          text: `计算结果为：${result}`,
        },
      ],
    };
  }
});

const transport = new StdioServerTransport();
await server.connect(transport);
